package com.cloudptt.wp.ui.more.advanced;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloudptt.api.comm.Const;
import com.cloudptt.api.product.Product;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.BaseActivity;
import com.cloudptt.wp.ui.MainActivity;
import com.mcptt.config.local.SysConfig;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class LanguageSelection extends BaseActivity {
	
	LanguageAdapter languageAdapter;
	ListView languageListView;
	TextView backView;
	int lastLan,curLan;
	
	private SysConfig sysConfig = null;
	
	List<String> languageList = null;
	private Map<Integer, Boolean> isSelected; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		setContentView( R.layout.language_selection );
		
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		this.sysConfig = product.getSysConfig();
//		sharedPreferences = this.getSharedPreferences(
//				Const.SHARED_SECTION_USERINFO, Context.MODE_WORLD_WRITEABLE);
		
		languageList = new ArrayList<String>();
		
		languageList.add( getString(R.string.system_language) );
		languageList.add("中文");
		languageList.add("English");
		
		if (isSelected != null)  
            isSelected = null;  
        isSelected = new HashMap<Integer, Boolean>();  
        for (int i = 0; i < languageList.size(); i++) {  
            isSelected.put(i, false);  
        }
        String lanStr = this.sysConfig.getCurLan();
		if(lanStr.equals(SysConfig.LAN_EN_US))
		{
			lastLan = curLan = 2;
		}
		else if(lanStr.equals(SysConfig.LAN_ZH_CN))
		{
			lastLan = curLan = 1;
		}
		isSelected.put(lastLan, true);
		
		backView = (TextView)findViewById(R.id.back);
		backView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) 
			{
				exitActivity();
				return false;
			}
		});
		
		
		languageListView = (ListView)findViewById(R.id.language_list);
		languageAdapter = new LanguageAdapter(this);
		languageListView.setAdapter( languageAdapter  );
		languageListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		languageAdapter.notifyDataSetChanged();
		languageListView.setOnItemClickListener( new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int pos,
					long id) {
				String lanStr = "";
                switch (pos) {
                    case 0:
                    	lanStr = Const.LAN_SYSTEM;
                        break;
                    case 1:
                    	lanStr = Const.LAN_ZH_CN;
                        break;
                    case 2:
                    	lanStr = Const.LAN_EN_US;
                        break;
                }
                curLan = pos;
                sysConfig.setCurLan(lanStr);
//                SharedPreferences.Editor editor = sharedPreferences.edit();
//                editor.putString(Const.SHARED_PROP_LANGUAGE, lanStr);
//                editor.commit();
                for(Integer p : isSelected.keySet()) {  
                    isSelected.put(p, false);  
                }  
                isSelected.put(pos, true);
                languageAdapter.notifyDataSetChanged();
//				changeAppLanguage(getResources(), pos );
//		        LanguageSelection.this.finish();
                
			}
		});
		initNotificationLayout();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		
		if(android.R.id.home == item.getItemId()){
			return true;
		}
		
		return true;
		//return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	
//	public static void changeAppLanguage(Resources resources, int pos) {
//        Configuration config = resources.getConfiguration();
//        DisplayMetrics dm = resources.getDisplayMetrics();
//        if (1 == pos ) {
//            config.locale = Locale.CHINA;
//        } 
//        else if(2==pos) {
//        	config.locale = Locale.ENGLISH;
//        }
//        else
//        {
//            config.locale = Locale.getDefault();
//        }
//        resources.updateConfiguration(config, dm);
//    }
	
	public class LanguageAdapter extends BaseAdapter{

		private LayoutInflater mInflater;
        
        
        public LanguageAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return languageList.size();
        }
 
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return languageList.get(position);
        }
 
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
 
        public final class ViewHolder{
            public TextView title;
            public CheckBox checkBox;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder=new ViewHolder();  
                convertView = mInflater.inflate(R.layout.language_item, null);
                holder.title = (TextView)convertView.findViewById(R.id.language_item);
                holder.checkBox = (CheckBox)convertView.findViewById(R.id.item_checkbox_state);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder)convertView.getTag();
            }

            holder.title.setText((String)languageList.get(position));
            holder.checkBox.setChecked(isSelected.get(position));
            
            return convertView;
        }
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitActivity();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void exitActivity()
	{
		if (lastLan != curLan) {
			ScreenManager
					.getScreenManager()
					.popAllActivityExceptOne(this,MainActivity.class);
		} else {
			ScreenManager.getScreenManager().popActivity();
		}
	}
//	public static void updateLanguage(String lan, Resources resources)
//	{
//		int pos = 0;
//        //
//        if( lan.equals(Const.LAN_ZH_CN) )
//        {
//        	pos = 1;
//        }
//        else if( lan.equals(Const.LAN_EN_US) )
//        {
//        	pos = 2;
//        }
//        changeAppLanguage( resources, pos);
//	}
}
